package net.guerlab.smart.platform.user.internal.controller.inside;

import io.swagger.annotations.ApiParam;
import net.guerlab.smart.platform.commons.util.BeanConvertUtils;
import net.guerlab.smart.platform.user.core.entity.UserOauthDTO;
import net.guerlab.smart.platform.user.core.exception.UserHasNotBoundException;
import net.guerlab.smart.platform.user.core.searchparams.UserOauthSearchParams;
import net.guerlab.smart.platform.user.service.service.UserOauthService;
import net.guerlab.web.result.ListObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 用户Oauth
 *
 * @author guer
 */
@RestController("/inside/userOauth")
@RequestMapping("/inside/userOauth")
public class UserOauthController {

    private UserOauthService service;

    @GetMapping("/{type}/{id}")
    public UserOauthDTO findOne(@ApiParam(value = "type", required = true) @PathVariable String type,
            @ApiParam(value = "id", required = true) @PathVariable Long id) {
        return service.getOneOptional(type, id).orElseThrow(UserHasNotBoundException::new).toDTO();
    }

    @PostMapping
    public ListObject<UserOauthDTO> findList(@RequestBody UserOauthSearchParams searchParams) {
        return BeanConvertUtils.toListObject(service.selectPage(searchParams));
    }

    @PostMapping("/all")
    public Collection<UserOauthDTO> findAll(@RequestBody UserOauthSearchParams searchParams) {
        return BeanConvertUtils.toList(service.selectAll(searchParams));
    }

    @Autowired
    public void setService(UserOauthService service) {
        this.service = service;
    }
}
